<?php
require_once '/../Controleur/Forum_controleur.php';
require_once '/../Controleur/User_controleur.php';

if (isset($_GET['idSection']) || isset($_SESSION['idSection'])) {
    if (isset($_GET['idSection'])) {
        $idSection = $_GET['idSection'];
        $_SESSION['idSection'] = $idSection;
    } else if (isset($_SESSION['idSection'])) {
        $idSection = $_SESSION['idSection'];
    }
    $listeTopic = Forum_controleur::afficherListeTopic($idSection);
    ?>
    <div class="Forum_vue">
        <h3>Forum Mapping Dead</h3>
        <div class='topic'>
            <h3>Topics </h3>         
            <table id='section'>
                <tr><td><label>Nom du Topic</label></td><td><label>Date de création</label></td></tr>
                <?php
                foreach ($listeTopic as $topic) {
                    $idTopic = $topic->getIdForumTopic();
                    $nomTopic = $topic->getNomTopic();
                    $dateCreation = $topic->getDateCreation();
                    ?>
                    <form  method='GET'><input type="hidden" name="idTopic" value="<?php echo $idTopic ?>"/></form>                          
                    <tr><td ><a onclick="page('Message_vue')" href="?idTopic=<?php echo $idTopic ?>"><?php echo $nomTopic ?></a></td>
                        <td><?php echo $dateCreation ?></td>
                        <?php
                        if (isset($_SESSION['username']) && ($_SESSION['statut'] == 2)) {
                            echo "<td><input type ='button' id='suppTopic' value='Supprimer Topic'></td>";
                        }
                        echo '</tr>';
                    }
                    ?>
            </table>
            <?php if (isset($_SESSION['username'])) { ?>
                <form id="addTopic" action="#">
                    <table>
                        <tr>
                            <td>
                                <input type="text" id="nomTopic" value=""/>                               

                            </td>
                            <td>
                                <input type="hidden" id="idSection" value="<?php echo $idSection ?>"/>                               
                            </td>
                            <td>
                                <button class="btnTopic" type="submit" >Ajouter un topic</button>
                            </td>
                        </tr>

                    </table> </form></div>
            <?php
        } else {
            echo "<br><h3>Veuillez vous connecter pour créer des Topics</h3>";
        }
    }
    ?>
    <script>
        function page(nomPage) {

            var chemin = 'src/Vue/' + nomPage + '.php';
            $.ajaxSetup({async: false});
            $.post("src/Vue/session.php", {"chemin": chemin});
            $.ajaxSetup({async: true});
            $("#contenu").load('src/Vue/' + nomPage + '.php');
            if(nomPage == "User_vue" || nomPage == "Carte_vue") {
			location.reload();
		}
        }
        $('#suppMess').on("click", function () {
            alert('Supp');
            $.ajaxSetup({async: false});
            $.post("src/Vue/verif.php",
                    {
                        idMessageSup: $("#idMessage").val()
                    });
            $.ajaxSetup({async: true});
            //        location.reload();

        });
        $('#addTopic').submit(function () {
            $.ajaxSetup({async: false});
            $.post("src/Vue/verif.php",
                    {
                        idSection: $("#idSection").val(),
                        nomTopic: $("#nomTopic").val()
                    });
            $.ajaxSetup({async: true});
            //        location.reload();

        });
        $('#addMessage').submit(function () {
            //            alert($("#newMessage").val());
            $.ajaxSetup({async: false});
            $.post("src/Vue/verif.php",
                    {
                        newMessage: $("#newMessage").val(),
                        idTopic: $("#idTopic").val()
                    });
            $.ajaxSetup({async: true});
            //        location.reload();

        });
    </script>